<?php

namespace app\controller\system;

use app\controller\BaseController;
use app\model\system\SysNotice;
use app\model\system\SysUserNotice;
use support\Request;
use support\Response;

class NoticeController extends BaseController
{

    public function create(Request $request): Response
    {
        $data = $request->post();
        $data["publisher_id"] = $request->currUser->userId;
        $data["create_by"] = $request->currUser->userId;
        $notice = SysNotice::create($data);
        if ($notice) {
            return $this->success($notice->toArray());
        } else {
            return $this->error('新增失败');
        }
    }

    public function form(int $id): Response
    {
        $notice = SysNotice::find($id);
        return $this->success($notice->toArray());
    }

    public function update(Request $request, int $id): Response
    {
        $data = $request->post();
        $notice = SysNotice::find($id);
        if (!$notice) {
            return $this->error('数据不存在');
        } else {
            if ($notice->save($data)) {
                return $this->success($notice->toArray());
            } else {
                return $this->error('更新失败');
            };
        }
    }


    public function detail(int $id): Response
    {
        $notice = SysNotice::with("publisher")->find($id);
        if (!$notice) {
            return $this->error('数据不存在');
        }
        return $this->success($notice->toArray());
    }


    public function delete(string $ids): Response
    {
        $idsArr = explode(",", $ids);
        if (SysNotice::destroy($idsArr)) {
            return $this->success(["count" => count($idsArr)]);
        } else {
            return $this->error("删除失败");
        };
    }

    public function page(Request $request): Response
    {
        $params = $request->get();
        extract($params);
        $list = SysNotice::with("publisher")->order("id", "desc");
        if (isset($title) && $title) {
            $list->whereLike("title", "%" . $title . "%");
        }
        if (isset($isRead) && $isRead) {
            $list->where("is_read", $isRead);
        }
        if (isset($userId) && $userId) {
            $list->whereFindInSet("target_user_ids", $userId);
        }
        if (isset($publishStatus) && $publishStatus) {
            $list->where("publish_status", $isDel);
        }

        $list = $list->paginate(['list_rows' => $pageSize, 'page' => $pageNum]);
        return $this->success(["list" => $list->items(), "total" => $list->total()]);
    }


    public function readAll(Request $request): Response
    {
        $userId = $request->currUser->userId;
        $userNotices = SysUserNotice::where("user_id", $userId)->where("is_read", 0)->select();
        $noticeIds = [];
        if ($userNotices) {
            foreach ($userNotices as $userNotice) {
                $userNotice->is_read = 1;
                $userNotice->read_time = date("Y-m-d H:i:s");
                $userNotice->save();
                $noticeIds[] = $userNotice->notice_id;
            }
        }
        return $this->success($noticeIds);
    }

    public function userNotices(Request $request): Response
    {
        $jwtUser = $request->currUser;
        $pageSize = $request->get("pageSize", 10);
        $pageNum = $request->get("pageNum", 1);
        $isRead = $request->get("isRead", 1);
        $list = SysUserNotice::where("user_id", $jwtUser->userId)
            ->where("is_read", $isRead)
            ->order("id", "desc")
            ->paginate(['list_rows' => $pageSize, 'page' => $pageNum]);
        $total = $list->total();
        return $this->success(["list" => $list->items(), "total" => $total]);
    }

    public function publish(int $id): Response
    {
        $notice = SysNotice::find($id);
        if (!$notice) {
            return $this->error('数据不存在');
        }
        if ($notice->save(["publish_status" => 1, "publish_time" => date("Y-m-d H:i:s")])) {
            return $this->success($notice->toArray());
        } else {
            return $this->error('发布失败');
        };
    }

    public function revoke(int $id): Response
    {
        $notice = SysNotice::find($id);
        if (!$notice) {
            return $this->error('数据不存在');
        }
        if ($notice->save(["publish_status" => 0, "publish_time" => null])) {
            return $this->success($notice->toArray());
        } else {
            return $this->error('撤销失败');
        }
    }
}
